package com.sxt.system.service.impl;

import com.sxt.system.constast.Constast;
import com.sxt.system.domain.Role;
import com.sxt.system.mapper.RoleMapper;
import com.sxt.system.service.IRoleService;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.vo.RoleVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

	private Log log = LogFactory.getLog(RoleServiceImpl.class);
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Transactional(readOnly = true)
	@Override
	public DataGridView loadAllRole(RoleVO roleVO) {
		QueryWrapper<Role> qw = new QueryWrapper<Role>();
		if(null!=roleVO) {
			qw.like(StringUtils.isNotBlank(roleVO.getName()), "name", roleVO.getName());
			qw.like(StringUtils.isNotBlank(roleVO.getRemark()), "remark", roleVO.getRemark());
		}else {
			log.info("roleVO为空");
		}
		Page<Role> page = new Page<>(roleVO.getPage(),roleVO.getLimit());
		this.roleMapper.selectPage(page, qw);
		return new DataGridView(page.getTotal(),page.getRecords());
	}

	@Override
	public Role addRole(Role role) {
		this.roleMapper.insert(role);
		return role;
	}

	@Override
	public Role updateRole(Role role) {
		this.roleMapper.updateById(role);
		return role;
	}

	@Override
	public boolean removeById(Serializable id) {
		//根据角色ID删除角色和权限中间表的数据
		this.roleMapper.deleteRolePermissionByRid(id);
		//根据角色ID删除角色和用户的关系
		this.roleMapper.deleteUserRoleByRoleId(id);
		return super.removeById(id);//删除角色
	}

	@Override
	public boolean removeByIds(Collection<? extends Serializable> idList) {
		//根据角色Id删除角色和权限中间表的数据
		if(idList.size() > 0) {
			for (Serializable id : idList) {
				//根据角色ID删除角色和用户的关系
				this.roleMapper.deleteUserRoleByRoleId(id);
				//根据角色ID删除角色和权限中间表的数据
				this.getBaseMapper().deleteRolePermissionByRid(id);
			}
		}
		
		return super.removeByIds(idList);
	}

	@Override
	public void saveRolePermission(Integer roleId, Integer[] pids) {
		//1根据角色ID删除对应的权限
		this.getBaseMapper().deleteRolePermissionByRid(roleId);
		//2保存关系
		if(null!=pids&&pids.length>0) {
			for (Integer pid : pids) {
				this.getBaseMapper().saveRolePermission(roleId,pid);
			}
		}
	}

	@Override
	public DataGridView queryRolesByUserId(Integer userId) {
		QueryWrapper<Role> qw = new QueryWrapper<Role>();
		qw.eq("available", Constast.AVAILABLE_TRUE);
		//查询所有可以的角色
		List<Role> allRoles = this.getBaseMapper().selectList(qw);
		
		//查询当前用户拥有的角色Id
		List<Integer> roleIds = this.getBaseMapper().selectRoleIdsByUserId(userId);
		
		List<Role> currentUserRoles = new ArrayList<Role>();
		if (roleIds!=null&&roleIds.size()!=0) {
			qw.in("id", roleIds);
			currentUserRoles = this.getBaseMapper().selectList(qw);
		}
		
		List<Map<String,Object>> res = new ArrayList<>();
		
		for (Role r1 : allRoles) {
			Boolean LAY_CHECKED = false;
			for (Role r2 : currentUserRoles) {
				if(r1.getId()==r2.getId()) {
					LAY_CHECKED=true;
					break;
				}
			}
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", r1.getId());
			map.put("name", r1.getName());
			map.put("remark", r1.getRemark());
			map.put("LAY_CHECKED", LAY_CHECKED);
			res.add(map);
		}
		
		return new DataGridView(res);
	}

	/**
	 * 根据用户ID查询用户拥有的角色名称
	 */
	@Override
	public List<String> queryRoleNamesByUserId(Integer id) {
		//查询当前用户拥有的角色ID
		List<Integer> roleIds = this.getBaseMapper().selectRoleIdsByUserId(id);
		QueryWrapper<Role> qw = new QueryWrapper<Role>();
		qw.eq("available", Constast.AVAILABLE_TRUE);
		List<Role> currentUserRole = new ArrayList<Role>();
		if(roleIds!=null&&roleIds.size()>0) {
			qw.in("id", roleIds);
			currentUserRole = this.getBaseMapper().selectList(qw);
		}
		List<String> roles = new ArrayList<String>();
		for (Role role : currentUserRole) {
			roles.add(role.getName());
		}
		return roles;
	}

}
